package com.zrrd.straw1.mapper;

import com.zrrd.straw1.entity.Permission;
import com.zrrd.straw1.entity.Role;
import com.zrrd.straw1.entity.User;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zrrd.straw1.vo.UserVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* <p>
    *  Mapper 接口
    * </p>
*
* @author zrrd
* @since 2024-03-03
*/
    @Repository
    public interface UserMapper extends BaseMapper<User> {

        @Select("select * from user where username = #{username}")
        User findUserByUsername(String username);


        //应该先在数据库测试 sql 语句没有问题
        //查询指定用户的所有角色
        @Select("SELECT   r.id,  r.name " +
                " FROM   USER  u  " +
                " JOIN   user_role  ur  ON u.id = ur.user_id  " +
                " JOIN   role    r          ON  ur.role_id = r.id  " +
                " WHERE  u.id = #{id} ")
        List<Role> findUserRoleById(Integer id);

        @Select("select id,username,nick_name from user where username = #{username}")
        UserVo findUserVoByUsername( String username );

        //提供给业务层访问的入口
        //1.提供的条件
        //2.得到的结果
        //3.见名知义的方法名
        //  特别注意：java中写 sql 语句换行时注意保留空格 ！
        @Select("SELECT  authority\n" +
                "FROM   user u JOIN user_role ur ON u.id = ur.user_id\n" +
                "JOIN   role r ON ur.role_id = r.id\n" +
                "JOIN   role_permission rp ON r.id = rp.role_id\n" +
                "JOIN   permission p ON rp.permission_id = p.id \n" +
                "WHERE  username = #{username}")
        List<Permission> findPermissionByUsername(String username);

    }
